package com.agile.leetcode.NumberEnglish;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author ChenZhangKun
 * @Date 2020/5/28 8:59
 */

/**
 * 给定一个整数，打印该整数的英文描述。
 *
 * 示例 1:
 *
 * 输入: 123
 * 输出: "One Hundred Twenty Three"
 *
 * 示例 2:
 *
 * 输入: 12345
 * 输出: "Twelve Thousand Three Hundred Forty Five"
 *
 * 示例 3:
 *
 * 输入: 1234567
 * 输出: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
 *
 * 示例 4:
 *
 * 输入: 1234567891
 * 输出: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/english-int-lcci
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class NumberEnglish {
    public static void main(String[] args) {
        String s = new Solution().numberToWords(99);
        System.out.println(s);
    }
}
class Solution {
    public String numberToWords(int num) {
        if(num == 0)
            return "Zero";
        List<String> res = new ArrayList<>();
        int hundred = 0;
        int thousand = 0;
        int million = 0;
        int billion = 0;
        if(num < 0){
            res.add("Negative");
            num = -num;
        }
        if(num > 0){
            hundred = num % 1000;
            num /= 1000;
            if(num > 0){
                thousand = num % 1000;
                num /= 1000;
                if(num > 0){
                    million = num % 1000;
                    num /= 1000;
                    if(num > 0){
                        billion = num % 1000;
                    }
                }
            }
        }
        if(billion > 0){
            for(String str:change(billion))
                res.add(str);
            res.add("Billion");
        }
        if(million > 0){
            for(String str:change(million))
                res.add(str);
            res.add("Million");
        }
        if(thousand > 0){
            for(String str:change(thousand))
                res.add(str);
            res.add("Thousand");
        }
        if(hundred > 0){
            for(String str:change(hundred))
                res.add(str);
        }
        StringBuilder ans = new StringBuilder();
        for(String s:res){
            if(ans.length() == 0)
                ans.append(s);
            else{
                ans.append(" " + s);
            }
        }
        return ans.toString();
    }
    private LinkedList<String> change(int num){
        LinkedList<String> list = new LinkedList<>();
        int n = num % 100;
        if(0 < n && n < 20){
            list.addFirst(num0_19[n]);
        }else if(n >= 20){
            int m = n % 10;
            int mm = n / 10;
            if(m > 0)
                list.addFirst(num0_19[m]);
            list.addFirst(num0_90[mm]);
        }
        if(num >= 100){
            list.addFirst("Hundred");
            list.addFirst(num0_19[num / 100]);
        }
        return list;
    }
    private String[] num0_19 = new String[]{ "Zero","One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen" };
    private String[] num0_90 = new String[]{ "Zero","Ten","Twenty","Thirty", "Forty", "Fifty", "Sixty",  "Seventy", "Eighty", "Ninety" };
}


